package com.cv.platform.service.message;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cv.framework.common.utils.object.BeanUtils;
import com.cv.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cv.framework.security.core.utils.SecurityFrameworkUtil;
import com.cv.platform.controller.platform.message.CvMessageController;
import com.cv.platform.controller.platform.message.vo.unreadCount.CvMessageTotalUnreadRespVO;
import com.cv.platform.controller.platform.message.vo.unreadCount.CvMessageUnreadCountRespVO;
import com.cv.platform.controller.platform.message.vo.unreadCount.CvMessageUnreadCountSaveReqVO;
import com.cv.platform.dal.dataobject.message.CvMessageUnreadCountDO;
import com.cv.platform.dal.mapper.message.CvMessageUnreadCountMapper;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 消息未读统计业务接口实现类
 *
 * @author Charles_XDXD
 */
@Service
public class CvMessageUnreadCountServiceImpl implements CvMessageUnreadCountService {

    @Resource
    private CvMessageUnreadCountMapper cvMessageUnreadCountMapper;

    @Resource
    @Lazy
    private CvMessageController cvMessageController;

    @Resource
    @Lazy
    private CvMessageService cvMessageService;

    @Resource
    private CvConversationVisibleService cvConversationVisibleService;

    @Override
    public CvMessageTotalUnreadRespVO getTotalCount(Long userId) {
        LambdaUpdateWrapper<CvMessageUnreadCountDO> wrapper = new LambdaUpdateWrapper<CvMessageUnreadCountDO>()
                .eq(CvMessageUnreadCountDO::getUserId, userId);

        List<CvMessageUnreadCountDO> unreadCountDOList = cvMessageUnreadCountMapper.selectList(wrapper);
        if (CollUtil.isEmpty(unreadCountDOList)) {
            return new CvMessageTotalUnreadRespVO().setTotalCount(0);
        }

        return new CvMessageTotalUnreadRespVO().setTotalCount(unreadCountDOList.stream().mapToInt(CvMessageUnreadCountDO::getUnreadCount).sum());
    }

    @Override
    public void increaseUnreadCount(CvMessageUnreadCountSaveReqVO reqVO) {
        if (ObjUtil.isNotEmpty(reqVO)) {
            CvMessageUnreadCountDO unreadCountDO = BeanUtils.toBean(reqVO, CvMessageUnreadCountDO.class);

            LambdaQueryWrapperX<CvMessageUnreadCountDO> wrapper = new LambdaQueryWrapperX<CvMessageUnreadCountDO>()
                    .eq(CvMessageUnreadCountDO::getUserId, reqVO.getUserId())
                    .eq(CvMessageUnreadCountDO::getConversationId, reqVO.getConversationId());
            CvMessageUnreadCountDO recordDO = cvMessageUnreadCountMapper.selectOne(wrapper);

            if (ObjUtil.isEmpty(recordDO)) {
                unreadCountDO.setUnreadCount(1);
                cvMessageUnreadCountMapper.insertOrUpdate(unreadCountDO);
            } else {
                recordDO.setUnreadCount(recordDO.getUnreadCount() + 1);
                cvMessageUnreadCountMapper.insertOrUpdate(recordDO);
            }

            cvConversationVisibleService.setVisible(reqVO.getConversationId(), reqVO.getUserId(), true);
            // 发送未读消息更新
            cvMessageController.sendEvent(reqVO.getUserId(), getTotalCount(reqVO.getUserId()));
        }
    }

    @Override
    public List<CvMessageUnreadCountRespVO> getUnreadCountList() {
        LambdaUpdateWrapper<CvMessageUnreadCountDO> wrapper = new LambdaUpdateWrapper<CvMessageUnreadCountDO>()
                .eq(CvMessageUnreadCountDO::getUserId, SecurityFrameworkUtil.getLoginUserId());

        return BeanUtils.toBean(cvMessageUnreadCountMapper.selectList(wrapper), CvMessageUnreadCountRespVO.class);
    }

    @Override
    public void clearUnreadCount(Long conversationId) {
        LambdaQueryWrapperX<CvMessageUnreadCountDO> wrapper = new LambdaQueryWrapperX<CvMessageUnreadCountDO>()
                .eq(CvMessageUnreadCountDO::getConversationId, conversationId)
                .eq(CvMessageUnreadCountDO::getUserId, SecurityFrameworkUtil.getLoginUserId());

        CvMessageUnreadCountDO unreadCountDO = cvMessageUnreadCountMapper.selectOne(wrapper);

        if (ObjUtil.isEmpty(unreadCountDO)) {
            unreadCountDO = new CvMessageUnreadCountDO();
        }

        unreadCountDO.setUserId(SecurityFrameworkUtil.getLoginUserId())
                .setConversationId(conversationId)
                .setUnreadCount(0);
        cvMessageUnreadCountMapper.insertOrUpdate(unreadCountDO);
        cvMessageService.setRead(conversationId);
    }

}
